package com.oceansoft.core.jxkh.dao.impl;

import com.hui.platform.system.utility.StringUtil;
import com.oceansoft.core.common.util.PageUtil;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.jxkh.dao.IKhyDao;
import com.oceansoft.core.jxkh.domain.Khy;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by OS on 2017/12/11.
 */

@Repository
public class KhyDaoImpl implements IKhyDao {

    @Resource
    private JdbcTemplate jdbcTemplate;


    @Override
    public Pagination findAllKhy(HashMap<String, String> map) {
        int offset = Integer.parseInt(map.get("offset").toString());
        int limit = Integer.parseInt(map.get("limit").toString());
        String sql = "select * from t_pskh_check_itema order by dbid";
        String count_sql = PageUtil.getCountSQL(sql.toString());
        int _total = jdbcTemplate.queryForInt(count_sql);
        String page_sql = PageUtil.getPageSQL(sql.toString(), offset, limit);
        List list = jdbcTemplate.queryForList(page_sql);
        return new Pagination(list, _total, limit, (offset / limit) + 1);
    }

    @Override
    public List<Khy> findAllKhy() throws Exception {
        String sql = "select * from t_pskh_check_itema order by dbid";
        List<Khy> list = jdbcTemplate.query(sql, new RowMapper<Khy>() {
            @Override
            public Khy mapRow(ResultSet rs, int args) throws SQLException {
                Khy khy = new Khy();
                khy.setDbid(rs.getInt("dbid"));
                khy.setName(rs.getString("name"));
                khy.setType(rs.getString("type"));
                khy.setScore(rs.getInt("score"));
                khy.setAmax(rs.getInt("amax"));
                khy.setAmin(rs.getInt("amin"));
                khy.setBmax(rs.getInt("bmax"));
                khy.setBmin(rs.getInt("bmin"));
                khy.setCmax(rs.getInt("cmax"));
                khy.setCmin(rs.getInt("cmin"));
                khy.setDmax(rs.getInt("dmax"));
                khy.setDmin(rs.getInt("dmin"));
                khy.setStatus(rs.getInt("status"));
                return khy;
            }
        });
        return list;
    }

    @Override
    public void editKhyInfo(Khy khy) {
        String sql = "update t_pskh_check_itema set name = ? , type = ? , score = ? , amax = ? , amin = ? ," +
                " bmax = ? , bmin = ? ,cmax = ? ,cmin = ? , dmax = ? , dmin = ? , status = ? where dbid = ?";
        jdbcTemplate.update(sql, khy.getName(), khy.getType(), khy.getScore(), khy.getAmax(),
                khy.getAmin(), khy.getBmax(), khy.getBmin(), khy.getCmax(), khy.getCmin(), khy.getDmax(), khy.getDmin(), khy.getStatus(), khy.getDbid());
    }

    @Override
    public Khy findKhyInfoByDbid(int dbid) {
        String sql = "select * from t_pskh_check_itema where dbid = ?";
        Khy khy = jdbcTemplate.queryForObject(sql, new Object[]{dbid}, new RowMapper<Khy>() {
            @Override
            public Khy mapRow(ResultSet rs, int args) throws SQLException {
                Khy khy = new Khy();
                khy.setDbid(rs.getInt("dbid"));
                khy.setName(rs.getString("name"));
                khy.setType(rs.getString("type"));
                khy.setScore(rs.getInt("score"));
                khy.setAmax(rs.getInt("amax"));
                khy.setAmin(rs.getInt("amin"));
                khy.setBmax(rs.getInt("bmax"));
                khy.setBmin(rs.getInt("bmin"));
                khy.setCmax(rs.getInt("cmax"));
                khy.setCmin(rs.getInt("cmin"));
                khy.setDmax(rs.getInt("dmax"));
                khy.setDmin(rs.getInt("dmin"));
                khy.setStatus(rs.getInt("status"));
                return khy;
            }
        });
        return khy;
    }

    @Override
    public void deleteKhyInfoByDbid(int dbid) {
        String sql = "delete from t_pskh_check_itema where dbid = ?";
        jdbcTemplate.update(sql, new Object[]{dbid});
    }

    @Override
    public void addKhyInfo(Khy khy) {
        String sql = "insert into t_pskh_check_itema(dbid,name,type,score,amax,amin,bmax" +
                ",bmin,cmax,cmin,dmax,dmin,status) values(SEQ_PSKH_CHECK_ITEMA.NEXTVAL,?,?,?,?,?,?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql, khy.getName(), khy.getType(), khy.getScore(), khy.getAmax(), khy.getAmin(), khy.getBmax(), khy.getBmin(), khy.getCmax(), khy.getCmin(), khy.getDmax(), khy.getDmin(), khy.getStatus());
    }

    @Override
    public void batchDeleteKhyInfo(final int[] dbids) {
        String sql = "delete from t_pskh_check_itema where dbid = ?";
        int[] delCounts = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setInt(1, dbids[i]);
            }

            @Override
            public int getBatchSize() {
                return dbids.length;
            }
        });
    }

    @Override
    public List findAllKhyName() {
        String sql = "select name,dbid from t_pskh_check_itema";
        List list = jdbcTemplate.queryForList(sql);
        return list;
    }
}
